package com.tofix.kidmall.service.impl;


import com.tofix.kidmall.common.CommonPage;
import com.tofix.kidmall.common.CommonResult;
import com.tofix.kidmall.entity.Cart;
import com.tofix.kidmall.entity.Goods;
import com.tofix.kidmall.exception.ApiExcetion;
import com.tofix.kidmall.repo.CartRepo;
import com.tofix.kidmall.repo.GoodsRepo;
import com.tofix.kidmall.service.CartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.Set;

@Service
@Transactional(rollbackFor = Exception.class)

public class CartServiceImpl implements CartService {
    private final static Logger logger = LoggerFactory.getLogger(CartServiceImpl.class);

    @Autowired
    private CartRepo cartRepo;
    @Autowired
    private GoodsRepo goodsRepo;

    @Override
    public Cart getCart(String id) {
        Optional<Cart> byId = cartRepo.findById(id);
        return byId.orElseGet(Cart::new);
    }

    @Override
    public Cart addCart(Cart cart) {
        //确定商品被选
        return null;
    }

    @Override
    public Integer deleteCart(String id) {
        return null;
   }

    @Override
    public Cart saveCart(Cart cart) {
        return cartRepo.save(cart);
    }

    @Override
    public CommonPage<Cart> getAllByPage(Integer currentPage, Integer pageSize) {
        return CommonPage.restPage(cartRepo.findAll(PageRequest.of(currentPage, pageSize)));
    }

    @Override
    public CommonResult<List<Cart>> getAll() {
        return CommonResult.success(cartRepo.findAll());
    }
}
